﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
using AForge.Video;
using AForge.Video.DirectShow;
using System.Threading;


namespace mycapturer
{
    public partial class Form1 : Form
    {
        private int framesCaptured; //已经捕获的视频帧数
        private int frameCount;     //需要捕获的总帧数
        private Stopwatch sw;       //计时器
        private VideoCaptureDevice captureAForge = null;    //AForge视频捕获对象
        private bool stopCapture;                           //是否停止捕获视频
        private object lockObject = new object();


        public Form1()
        {
            InitializeComponent();
            FilterInfoCollection videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            if (videoDevices.Count > 0)
            {
                int idx = 0;
                foreach (FilterInfo device in videoDevices)
                {
                    //cmbCaptureDevice.Items.Add(new DeviceInfo(device.Name, device.MonikerString, idx, FilterCategory.VideoInputDevice));
                    idx++;
                    captureAForge = new VideoCaptureDevice(device.MonikerString);
                    captureAForge.NewFrame += new NewFrameEventHandler(captureAForge_NewFrame);
                    break;
                    
                    //captureAForge.Start();
                    //...
                    //captureAForge.SignalToStop();
                }
                //cmbCaptureDevice.SelectedIndex = 0;
            }
        }

        private void btn_Start_Click(object sender, EventArgs e)
        {
            if (captureAForge != null)
            {
                captureAForge.Start();
            }
        }

        private void captureAForge_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            eventArgs.Frame.Save(Path.Combine("c:\\temp", DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss_fff")+".bmp"));
            pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();
        }

        private void btn_Stop_Click(object sender, EventArgs e)
        {
            if (captureAForge != null)
            {
                captureAForge.SignalToStop();
            }
        }
    }
}
